package com.neusoft.elmboot.mapper;

import java.math.BigDecimal;
import com.neusoft.elmboot.po.Wallet;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

@Mapper
public interface WalletMapper {
    @Select("SELECT * FROM wallet WHERE user_id = #{userId}")
    Wallet getWalletByUserId(String userId);

    @Update("UPDATE wallet SET balance = balance + #{amount}, frozen_balance = frozen_balance - #{amount} WHERE wallet_id = #{walletId}")
    int unfreezeBalance(Integer walletId, BigDecimal amount);

    @Insert("INSERT INTO wallet (user_id, balance, frozen_balance, credit_limit, is_vip) VALUES (#{userId}, #{balance}, #{frozenBalance}, #{creditLimit}, #{isVip})")
    @Options(useGeneratedKeys = true, keyProperty = "walletId", keyColumn = "walletId")
    int insertWallet(Wallet wallet);

    @Update("UPDATE wallet SET balance = #{balance}, frozen_balance = #{frozenBalance}, credit_limit = #{creditLimit}, is_vip = #{isVip} WHERE wallet_id = #{walletId}")
    int updateWallet(Wallet wallet);
}
